package code.cando;


/**
 * author : yeswater
 * date : 2023/12/6
 * <p>
 * 合并两个有序链表
 * 0424 04-44
 */

public class T021 {
    public static void main(String[] args) {

    }

    /**
     * 1 if-else 写清楚  
     * 2 cur 指针记得后移
     */
    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode per = new ListNode();
        ListNode cur = per;
        while (list1 != null && list2 != null) {
            int val1 = list1.val;
            int val2 = list2.val;
            if (val1 > val2) {
                cur.next = new ListNode(val2);
                list2 = list2.next;
            } else {
                cur.next = new ListNode(val1);
                list1 = list1.next;
            }
            cur = cur.next;
        }
        if (list1 == null) {
            cur.next = list2;
        }
        if (list2 == null) {
            cur.next = list1;
        }
        return per.next;
    }

    private static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
